package com.ayu.shortlink.settlement.controller;

import com.ayu.shortlink.settlement.common.convention.result.Result;
import com.ayu.shortlink.settlement.common.convention.result.Results;
import com.ayu.shortlink.settlement.dto.req.OrderCreateReqDTO;
import com.ayu.shortlink.settlement.dto.req.OrderPageQueryReqDTO;
import com.ayu.shortlink.settlement.dto.resp.OrderCreateRespDTO;
import com.ayu.shortlink.settlement.dto.resp.OrderPageQueryRespDTO;
import com.ayu.shortlink.settlement.dto.resp.OrderQueryRespDTO;
import com.ayu.shortlink.settlement.service.OrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

/**
 * 用户订单控制器
 */
@RestController
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 创建订单 -> 返回订单信息
     * // TODO 避免用户瞬时双击创建多个订单
     */
    @PostMapping("/api/short-link/settlement/v1/order/create")
    public Result<OrderCreateRespDTO> createOrder(@RequestBody OrderCreateReqDTO requestParam){
        return Results.success(orderService.createOrder(requestParam));
    }

    /**
     * 查询订单
     */
    @GetMapping("/api/short-link/settlement/v1/order/query/{orderSn}")
    public Result<OrderQueryRespDTO> queryByOrderSn(@PathVariable("orderSn") String orderSn){
        return Results.success(orderService.queryByOrderSn(orderSn));
    }

    /**
     * 分页查询用户所有订单
     */
    @GetMapping("/api/short-link/settlement/v1/order/page")
    public Result<IPage<OrderPageQueryRespDTO>> pageQuery(@RequestBody OrderPageQueryReqDTO requestParam){
        return Results.success(orderService.pageQuery(requestParam));
    }

    /**
     * 取消订单
     */
    @DeleteMapping("/api/short-link/settlement/v1/order/cancel")
    public Result<Void> cancelOrder(@Param("orderSn") String orderSn){
        orderService.cancelOrder(orderSn);
        return Results.success();
    }
}
